package com.huikeportal.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huikeportal.controller.common.Tools;
import com.huikeportal.model.RegionModel;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectSiteService;

@Controller
public class ChangeSiteController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private SelectSiteService selectSiteService;

	/**
	 * ajax查询集团&地区列表
	 * 
	 * @param jt
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/ajaxSearchJt")
	public Map<String, Object> ajaxSearchJt(String jt) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		if (jt != null && !"".equals(jt)) {
			jt = URLDecoder.decode(jt, "UTF-8");
		}
		SqlParameter sql = new SqlParameter();
		sql.setString(userInfo.getUserId());
		sql.setStringChar(jt);
		List<SiteAllInfo> jtList = selectSiteService.getJtDqList(sql);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("jtList", jtList);
		return map;
	}

	@ResponseBody
	@RequestMapping("/ajaxSearchQyLcDp")
	public Map<String, Object> ajaxSearchQyLcDp(String gc, String qy,
			String lc, String dp, String qyName, String lcName, String dpName,
			String siteId) throws Exception {
		boolean flag = false;
		boolean isShow = true;// 是否显示区域、楼层
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		RegionModel regionModel = new RegionModel();
		Map<String, Object> map = new HashMap<String, Object>();
		List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
		if (gc != null && !"".equals(gc)) {
			regionModel.setGcSiteId(gc);
			flag = Tools.isHaveGcPer(gcList, gc);
			// 如果是根据店铺反查的广场，则加入店铺集合作为筛选条件
			if (!flag) {
				List<SiteAllInfo> dpList = loginService.getDpSiteList(userInfo);
				isShow = false;
				regionModel.setDpString(Tools.pinjieId(dpList));
			}
		}
		if (qy != null && !"".equals(qy)) {
			regionModel.setQySiteId(qy);
		}
		if (lc != null && !"".equals(lc)) {
			regionModel.setLcSiteId(lc);
		}
		if (dp != null && !"".equals(dp)) {
			regionModel.setDpSiteId(dp);
		}
		if (qyName != null && !"".equals(qyName)) {
			regionModel.setQySiteName(URLDecoder.decode(qyName, "UTF-8"));
		}
		if (lcName != null && !"".equals(lcName)) {
			regionModel.setLcSiteName(URLDecoder.decode(lcName, "UTF-8"));
		}
		if (dpName != null && !"".equals(dpName)) {
			regionModel.setDpSiteName(URLDecoder.decode(dpName, "UTF-8"));
		}
		if (siteId != null && !"".equals(siteId)) {
			SiteCommon siteCommon = selectSiteService.querySiteInfo(siteId);
			if ("300".equals(siteCommon.getType())) {
				regionModel.setGcSiteId(siteId);
			} else if ("400".equals(siteCommon.getType())) {
				regionModel.setQySiteId(siteId);
			} else if ("500".equals(siteCommon.getType())) {
				regionModel.setLcSiteId(siteId);
			} else if ("600".equals(siteCommon.getType())) {
				regionModel.setDpSiteId(siteId);
			}
		}
		List<SiteAllInfo> qySiteList = new ArrayList<SiteAllInfo>();
		List<SiteAllInfo> lcSiteList = new ArrayList<SiteAllInfo>();
		// 如果是根据店铺反查的广场，不显示区域和楼层信息
		if (isShow) {
			qySiteList = selectSiteService.getTwiceQy(regionModel);
			lcSiteList = selectSiteService.getTwiceLc(regionModel);
		}
		List<SiteAllInfo> dpSiteList = selectSiteService
				.getTwiceDp(regionModel);
		map.put("qyList", qySiteList);
		map.put("lcList", lcSiteList);
		map.put("dpList", dpSiteList);
		return map;
	}

}
